home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / misc / configf.c < prev    next >
C/C++ Source or Header  |  1996-08-03  |  9KB  |  389 lines

  1. #pragma implementation
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <sys/stat.h>
  6. #include <pwd.h>
  7. #include <grp.h>
  8. #include "misc.h"
  9. #include "../userconf/userconf.h"
  10. #include "../paths.h"
  11. #include "misc.m"
  12.  
  13.  
  14. static HELP_FILE help_configf ("misc","configf");
  15. static CONFIG_FILE *first = NULL;
  16. static int nbconfig=0;
  17.  
  18. PRIVATE void CONFIG_FILE::init(
  19.     const char *_path,
  20.     int _status,
  21.     const char *_owner,
  22.     const char *_group,
  23.     int _perm)
  24. {
  25.     owner = _owner;
  26.     group = _group;
  27.     perm = _perm;
  28.     stdpath = _path;
  29.     realpath = NULL;    // fixpath() will look it up
  30.                 // later when more is initialised
  31.                 // This also avoid a massive query
  32.                 // to the linuxconf_getval() function
  33.                 // at startup time.
  34.     status = _status;
  35.     next = first;
  36.     first = this;
  37.     nbconfig++;
  38. }
  39. /*
  40.     Record the spec of a config file that is maintain (or used) by linuxconf
  41. */
  42. PUBLIC CONFIG_FILE::CONFIG_FILE(
  43.     const char *_path,
  44.     HELP_FILE &_helpfile,
  45.     int _status) : helpf(_helpfile)
  46. {
  47.     /* #Specification: configuration files / default permissions
  48.         Unless explicitly stated, the owner and permissions
  49.         of every configuration file produced and maintained
  50.         by linuxconf are
  51.  
  52.         #
  53.         rw-r--r--    root    root
  54.         #
  55.     */
  56.     init (_path,_status,"root","root",0644);
  57. }
  58.  
  59. /*
  60.     Record the spec of a config file that is maintain (or used) by linuxconf
  61. */
  62. PUBLIC CONFIG_FILE::CONFIG_FILE(
  63.     const char *_path,
  64.     HELP_FILE &_helpfile,
  65.     int _status,
  66.     const char *_owner,
  67.     const char *_group,
  68.     int _perm) : helpf(_helpfile)
  69. {
  70.     init (_path,_status,_owner,_group,_perm);
  71. }
  72. /*
  73.     Set the permissions and owner of a file to the same value as
  74.     the configuration file, if it has such a requirement.
  75.  
  76.     Return -1 if any error.
  77. */
  78. PUBLIC int CONFIG_FILE::setperm (const char *fpath)
  79. {
  80.     int ret = 0;
  81.     if (owner != NULL){
  82.         struct passwd *pw = getpwnam(owner);
  83.         struct group *gr = getgrnam(group);
  84.         ret = -1;
  85.         if (pw == NULL){
  86.             xconf_error (MSG_U(E_SETOWNER
  87.                 ,"Can't set ownership of file\n"
  88.                  "%s\n\n"
  89.                  "User %s does not exist\n")
  90.                 ,fpath,owner);
  91.         }else if (gr == NULL){
  92.             xconf_error (MSG_U(E_SETGROUP
  93.                 ,"Can't set group ownership of file\n"
  94.                  "%s\n\n"
  95.                  "Group %s does not exist\n")
  96.                 ,fpath,group);
  97.         }else if (chown (fpath,pw->pw_uid,gr->gr_gid) != -1
  98.             && chmod (fpath,perm) != -1){
  99.             ret = 0;
  100.         }
  101.     }
  102.     return ret;
  103. }
  104.  
  105. static char CONFIG[]="config";
  106.  
  107. PRIVATE void CONFIG_FILE::fixpath() const
  108. {
  109.     if (realpath == NULL){
  110.         /* #Specification: /etc/conf.linuxconf / can't be moved
  111.             Given that /etc/conf.linuxconf is used to store
  112.             almost everything which do not have a standard
  113.             home (configuration file), including the
  114.             the corrected path of the configuration themselves
  115.             it is not possible to change the location of
  116.             /etc/conf.linuxconf.
  117.         */
  118.         CONFIG_FILE *pt = (CONFIG_FILE*)this;
  119.         if (strcmp(stdpath,ETC_CONF_LINUXCONF)==0){
  120.             pt->realpath = stdpath;
  121.         }else{
  122.             const char *path = linuxconf_getval (CONFIG,stdpath);
  123.             if (path == NULL) path = stdpath;
  124.             pt->realpath = strdup(path);
  125.         }
  126.     }
  127. }
  128. /*
  129.     Open the configuration file with permission checking.
  130.     It may even ask the user for the root password.
  131. */
  132. PUBLIC FILE *CONFIG_FILE::fopen(const char *mode)
  133. {
  134.     FILE *ret = NULL;
  135.     fixpath();
  136.     if (strcmp(mode,"r")==0 && (status & CONFIGF_OPTIONNAL) != 0){
  137.         ret = ::fopen (realpath,mode);
  138.     }else{
  139.         ret = xconf_fopencfg(realpath,mode);
  140.         setperm(realpath);
  141.     }
  142.     return ret;
  143. }
  144. /*
  145.     Open the temp configuration file with permission checking.
  146.     It may even ask the user for the root password.
  147. */
  148. PUBLIC FILE *CONFIG_FILE::fopen(const char *temp, const char *mode)
  149. {
  150.     FILE *ret = xconf_fopencfg(temp,mode);
  151.     setperm(temp);
  152.     return ret;
  153. }
  154.  
  155. /*
  156.     Return the path of the configuration file
  157. */
  158. PUBLIC const char *CONFIG_FILE::getpath() const
  159. {
  160.     fixpath();
  161.     return realpath;
  162. }
  163. /*
  164.     Return the standard path of the configuration file
  165. */
  166. PUBLIC const char *CONFIG_FILE::getstdpath() const
  167. {
  168.     fixpath();
  169.     return stdpath;
  170. }
  171. /*
  172.     Return the path of the help for that configuration file
  173. */
  174. PUBLIC const char *CONFIG_FILE::gethelp() const
  175. {
  176.     return helpf.getpath();
  177. }
  178. /*
  179.     Return != 0 if the configuration file is managed by linuxconf.
  180.     Some configuration file are only read (expect to be there
  181.     and probably never edited by the user.
  182. */
  183. PUBLIC int CONFIG_FILE::is_managed() const
  184. {
  185.     return status & CONFIGF_MANAGED;
  186. }
  187. /*
  188.     Return != 0 if the configuration file is optionnal.
  189. */
  190. PUBLIC int CONFIG_FILE::is_optionnal() const
  191. {
  192.  
  193.     return status & CONFIGF_OPTIONNAL;
  194. }
  195. /*
  196.     Return != 0 if the configuration file is generated by linuxconf.
  197. */
  198. PUBLIC int CONFIG_FILE::is_generated() const 
  199. {
  200.     return status & CONFIGF_GENERATED;
  201. }
  202. /*
  203.     Return != 0 if the configuration file is erased at boot time.
  204. */
  205. PUBLIC int CONFIG_FILE::is_erased() const 
  206. {
  207.     return status & CONFIGF_ERASED;
  208. }
  209. /*
  210.     Return != 0 if the configuration file is probed by linuxconf.
  211.     probing is simply to check its modification time.
  212. */
  213. PUBLIC int CONFIG_FILE::is_probed() const
  214. {
  215.     return status & CONFIGF_PROBED;
  216. }
  217.  
  218. /*
  219.     Check if the file or directory exist.
  220.     Return != 0 if yes.
  221. */
  222. int file_exist (const char *path)
  223. {
  224.     struct stat st;
  225.     return stat(path,&st)!=-1;
  226. }
  227. /*
  228.     Check if the configuration file do exist.
  229.     Return != 0 if yes.
  230. */
  231. PUBLIC int CONFIG_FILE::exist() const
  232. {
  233.     fixpath();
  234.     return file_exist (realpath);
  235. }
  236.  
  237. /*
  238.     Return the modification date of a file
  239. */
  240. long file_date (const char *path)
  241. {
  242.     struct stat buf;
  243.     long ret = -1;
  244.     if (stat(path,&buf)!=-1){
  245.         ret = buf.st_mtime;
  246.     }
  247.     return ret;
  248. }
  249.  
  250. /*
  251.     Get the modification time of a configuration file.
  252. */
  253. PUBLIC long CONFIG_FILE::getdate() const
  254. {
  255.     fixpath();
  256.     return file_date (realpath);
  257. }
  258. /*
  259.     Get the modification time of a configuration file.
  260. */
  261. PUBLIC int CONFIG_FILE::unlink() const
  262. {
  263.     fixpath();
  264.     return ::unlink (realpath);
  265. }
  266.  
  267.  
  268. PUBLIC void CONFIG_FILE::editpath()
  269. {
  270.     SSTRING p(getpath());
  271.     while (1){
  272.         DIALOG dia;
  273.         dia.newf_str (MSG_U(F_CORRPATH,"Correct path"),p);
  274.         int nof = 0;
  275.         MENU_STATUS code = dia.edit (
  276.             MSG_U(T_MODCONFPATH,"Modifying a config file path")
  277.             ,MSG_U(I_MODCONFPATH
  278.              ,"You can redefined the path of a\n"
  279.              "configuration file. If you do so\n"
  280.              "Linuxconf will use the new path from now on.\n"
  281.              "\n"
  282.              "Be advise that other utilities are on there\n"
  283.              "own and may forget to notice. Unless you\n"
  284.              "really knows what you are doing, don't play\n"
  285.              "here.\n")
  286.             ,helpf
  287.             ,nof
  288.             ,MENUBUT_RESET|MENUBUT_ACCEPT|MENUBUT_CANCEL);
  289.         if (code == MENU_ESCAPE || code == MENU_CANCEL){
  290.             break;
  291.         }else if (code == MENU_RESET){
  292.             p.setfrom (stdpath);
  293.         }else if (code == MENU_ACCEPT){
  294.             if (p.is_empty()) p.setfrom (stdpath);
  295.             free ((char*)realpath);
  296.             realpath = NULL;
  297.             if (p.cmp(stdpath)==0){
  298.                 linuxconf_removeall (CONFIG,stdpath);
  299.             }else{
  300.                 linuxconf_replace (CONFIG,stdpath,p);
  301.             }
  302.             linuxconf_save();
  303.             break;
  304.         }
  305.     }
  306. }
  307.  
  308. static int cmp_config(const void *pt1, const void *pt2)
  309. {
  310.     CONFIG_FILE *p1 = *(CONFIG_FILE**)pt1;
  311.     CONFIG_FILE *p2 = *(CONFIG_FILE**)pt2;
  312.     return strcmp(p1->getstdpath(),p2->getstdpath());
  313. }
  314.  
  315. /*
  316.     List all config file managed by this system
  317. */
  318. void configf_show()
  319. {
  320.     CONFIG_FILE *tb[200];
  321.     {
  322.         CONFIG_FILE *f = first;
  323.         int no = 0;
  324.         while (f != NULL){
  325.             tb[no++] = f;
  326.             f = f->next;
  327.         }
  328.         qsort (tb,nbconfig,sizeof(CONFIG_FILE*),cmp_config);
  329.     }
  330.     int choice = 0;
  331.     while (1){
  332.         DIALOG dia;
  333.         for (int i=0; i<nbconfig; i++){
  334.             CONFIG_FILE *f = tb[i];
  335.             char type[6] = "     ";
  336.             if (f->is_erased())    type[0] = 'E';
  337.             if (f->is_generated()) type[1] = 'G';
  338.             if (f->is_managed())   type[2] = 'M';
  339.             if (f->is_optionnal()) type[3] = 'O';
  340.             if (f->is_probed())    type[4] = 'P';
  341.             const char *path = f->getpath();
  342.             const char *stdp = f->getstdpath();
  343.             char buf[80];
  344.             if (strcmp(path,stdp)==0){
  345.                 strcpy (buf,stdp);
  346.             }else{
  347.                 sprintf (buf,"%s (%s)",stdp,path);
  348.             }
  349.             dia.new_menuitem (type,buf);
  350.         }
  351.         MENU_STATUS code = dia.editmenu (
  352.             MSG_U(T_LISTCONF,"List of configuration files")
  353.             ,MSG_U(I_LISTCONF
  354.              ,"This is the list of all file managed\n"
  355.               "by linuxconf. For each file, you can access\n"
  356.               "directly a help file describing its purpose.\n"
  357.               "The letters preceding the file name indicate\n"
  358.               "how this file is managed by Linuxconf. Press help\n"
  359.               "for more info")
  360.             ,help_configf
  361.             ,choice,MENUBUT_EDIT);
  362.         if (code == MENU_QUIT || code == MENU_ESCAPE){
  363.             break;
  364.         }else{
  365.             CONFIG_FILE *cfgf = tb[choice];
  366.             if (code == MENU_EDIT){
  367.                 if (perm_rootaccess (
  368.                     MSG_U(P_MODCONF,"modify a configuration file path"))){
  369.                     cfgf->editpath();
  370.                 }
  371.             }else{
  372.                 dialog_textbox (cfgf->getpath(),cfgf->gethelp());
  373.             }
  374.         }
  375.     }
  376. }
  377.  
  378. /*
  379.     Erase some config file at boot time.
  380. */
  381. void configf_booterase()
  382. {
  383.     CONFIG_FILE *f = first;
  384.     while (f != NULL){
  385.         if (f->is_erased()) f->unlink();
  386.         f = f->next;
  387.     }
  388. }
  389.